VERSION := 0.5
CFLAGS := -O0 -g -Wall -DVERSION='"$(VERSION)"' -I/usr/include/mozjs
LFLAGS := -lsqlite3 -ljs -lz

BUILD := Makefile
SOURCES := src/bindings.c src/database.c src/javascript.c src/main.c \
	src/telnet.c src/timer.c
DOC := COPYING README
DATA := lib/mud.sql lib/main.js lib/greeting.txt

EXTRA := src/marray.c src/stdlib.js src/jsmud.h
OBJECTS := $(SOURCES:src/%.c=build/%.o) build/stdlib.o

all: build/touch lib/mud.db jsmud

build/touch:
	[ -d build ] || mkdir build
	touch $@

lib/mud.db:
	sqlite3 $@ < lib/mud.sql || rm -f $@

build/%.o: src/%.c src/jsmud.h Makefile
	$(CC) $(CFLAGS) -c -o $@ $<

build/marray: build/marray.o Makefile
	$(CC) $(CFLAGS) -o $@ $<

build/stdlib.c: src/stdlib.js build/marray Makefile
	build/marray $< js_stdlib > $@

jsmud: $(OBJECTS) Makefile
	$(CC) -o jsmud $(OBJECTS) $(LFLAGS)

clean:
	rm -f jsmud
	rm -fr build

dist:
	[ ! -d jsmud-$(VERSION) ] || rm -fr jsmud-$(VERSION)
	mkdir jsmud-$(VERSION)/
	mkdir jsmud-$(VERSION)/src/
	mkdir jsmud-$(VERSION)/lib/
	cp $(BUILD) jsmud-$(VERSION)/
	cp $(DOC) jsmud-$(VERSION)/
	cp $(SOURCES) jsmud-$(VERSION)/src/
	cp $(EXTRA) jsmud-$(VERSION)/src/
	cp $(DATA) jsmud-$(VERSION)/lib/
	tar -zcf jsmud-$(VERSION).tgz jsmud-$(VERSION)/
	rm -fr jsmud-$(VERSION)/
